<?php
class PhpExcelHelper{

    public static function save_excel($file_name ,$data,$title_width,$height){
        require_once 'PHPExcel.php';

        require_once 'PHPExcel/Writer/Excel5.php';     // 用于其他低版本xls

        // or
        ////require_once'PHPExcel/Writer/Excel2007.php'; // 用于excel-2007 格式
        // 创建一个处理对象实例
        $objExcel = new PHPExcel();
        // 创建文件格式写入对象实例, uncomment
        $objWriter = new PHPExcel_Writer_Excel5($objExcel);     // 用于其他版本格式
        // or
        ////$objWriter = newPHPExcel_Writer_Excel2007($objExcel); // 用于2007 格式
        $objProps = $objExcel->getProperties ();
        //设置创建者
        $objProps->setCreator ( 'anjuke');
        //设置最后修改者
        $objProps->setLastModifiedBy("anjuke");
        //描述
        $objProps->setDescription("anjuke");
        //设置标题
        $objProps->setTitle ( '管理器' );
        //设置题目
        $objProps->setSubject("OfficeXLS Test Document, Demo");
        //设置关键字
        $objProps->setKeywords ( '管理器' );
        //设置分类
        $objProps->setCategory ( "Test");
        //工作表设置
        $objExcel->setActiveSheetIndex( 0 );
        $objActSheet = $objExcel->getActiveSheet ();

        //标题  自动设置单元格宽度   例：
        foreach($title_width as $index => $val){
            $k = self::_get_index($index);
            $objActSheet->setCellValue ( $k.'1', $val['name']);
            $objActSheet->getColumnDimension($k)->setWidth($val['width']);
        }
        foreach ($height as $index => $val){
            $objActSheet->getRowDimension($index)->setRowHeight($val);
        }
        //单元格赋值   例：
        foreach($data as $index => $val){
            foreach($val as $k=> $v){
                $objActSheet->setCellValue ( $k, $v);
            }
        }


        //导出的文件名

        $outputFileName =  $file_name;

        //文件直接输出到浏览器

        header ( 'Pragma:public');

        header ( 'Expires:0');

        header ( 'Cache-Control:must-revalidate,post-check=0,pre-check=0');

        header ( 'Content-Type:application/force-download');

        header ( 'Content-Type:application/vnd.ms-excel');

        header ( 'Content-Type:application/octet-stream');

        header ( 'Content-Type:application/download');

        header ( 'Content-Disposition:attachment;filename='. $outputFileName );

        header ( 'Content-Transfer-Encoding:binary');

        $objWriter->save ( 'php://output');
    }
    public static function _get_index($in){
        $word = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
        if($in<26){
            return $word[$in];
        }else{
            $f = $in/26-1;
            $l = $in%26;
            return $word[$f].$word[$l];
        }
    }
}